package com.example.waypoint;

import utility.MyOverlays;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MapsActivity extends MapActivity {
	LinearLayout linearLayout;
	MapView mapView;
	ImageButton imageButtomHome;
	LocationManager locationManager; 
	MapController mapController;
	private MyOverlays itemizedoverlay;
	private String provider;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        
        mapView = (MapView) findViewById(R.id.mapview);
        
        mapView.setBuiltInZoomControls(true);
        mapController = mapView.getController();
        mapController.setZoom(16);
        
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		//set default location
		Criteria criteria = new Criteria();
		provider = locationManager.getBestProvider(criteria, false);
		Location location = locationManager.getLastKnownLocation(provider);
		if (location != null) 
		{
			int lat = (int) (location.getLatitude() * 1E6);
			int lng = (int) (location.getLongitude() * 1E6);
			
			GeoPoint point = new GeoPoint(lat, lng);
			mapController.setCenter(point);
		}
		//get location updates
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new GeoUpdateHandler());

		
		Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
		itemizedoverlay = new MyOverlays(drawable);
		createMarker();
        
        /*List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
        MyOverlays itemizedoverlay = new MyOverlays(drawable, this);
        GeoPoint point = new GeoPoint(-2144601,-79967651);
        OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);*/
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_maps, menu);
        return true;
    }
    
    public class GeoUpdateHandler implements LocationListener {

		public void onLocationChanged(Location location) {
		
				int lat = (int) (location.getLatitude() * 1E6);
				int lng = (int) (location.getLongitude() * 1E6);
				
				GeoPoint point = new GeoPoint(lat, lng);
				createMarker();
				mapController.setZoom(16);
				mapController.setCenter(point);
				
				/*GeoPoint pointUser = new GeoPoint(lat,lng);
		    	OverlayItem itemUser = new OverlayItem(pointUser, "", "");
				
				itemsLocations.addOverlay(itemUser);
				mapOverlays.add(itemsLocations);
				mapController.animateTo(pointUser);
				
		    	//mapController.setCenter(pointUser);*/
			
		}

		public void onProviderDisabled(String provider) {
		}

		public void onProviderEnabled(String provider) {
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	}

	
	
	

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	private void createMarker() {
		GeoPoint p = mapView.getMapCenter();
		OverlayItem overlayitem = new OverlayItem(p, "", "");
		itemizedoverlay.addOverlay(overlayitem);
		mapView.getOverlays().add(itemizedoverlay);
	}
}
